package com.falm.picturebackend.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.falm.picturebackend.entity.DTO.*;
import com.falm.picturebackend.entity.common.PageRequest;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import com.falm.picturebackend.annotation.AuthCheck;
import com.falm.picturebackend.common.BaseResponse;
import com.falm.picturebackend.common.ResultUtils;
import com.falm.picturebackend.entity.VO.UserVO;
import com.falm.picturebackend.entity.constant.UserConstant;
import com.falm.picturebackend.service.UserService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 用户接口
 * 
 * @author Administrator
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户接口")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/registry")
    @Operation(summary = "用户注册")
    public BaseResponse<String> registry(@RequestBody UserRegisterDTO userRegisterDTO) {
        userService.userRegister(userRegisterDTO);
        return ResultUtils.success();
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public BaseResponse<String> login(@RequestBody UserLoginDTO userLoginDTO) {
        String token = userService.userLogin(userLoginDTO);
        return ResultUtils.success(token);
    }

    @GetMapping("getLoginUser")
    @Operation(summary = "获取当前登录用户")
    public BaseResponse<UserVO> getLoginUser(HttpServletRequest request) {
        UserVO userVO = userService.getLoginUser(request);
        return ResultUtils.success(userVO);
    }

    @PostMapping("/logout")
    @Operation(summary = "用户登出")
    public BaseResponse<String> logout(HttpServletRequest request) {
        userService.logout(request);
        return ResultUtils.success();
    }

    @PostMapping("/addUser")
    @Operation(summary = "新增用户信息")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Void> addUser(@RequestBody UserAddDTO userAddDTO) {
        userService.addUser(userAddDTO);
        return ResultUtils.success();
    }

    @PostMapping("/deleteUser")
    @Operation(summary = "删除用户信息")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Void> deleteUser(@RequestBody UserDeleteDTO userDeleteDTO) {
        userService.deleteUser(userDeleteDTO);
        return ResultUtils.success();
    }

    @PostMapping("/updateUser")
    @Operation(summary = "更新用户信息")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Void> updateUser(@RequestBody UserUpdateDTO userUpdateDTO) {
        userService.updateUser(userUpdateDTO);
        return ResultUtils.success();
    }

    @PostMapping("/getUser")
    @Operation(summary = "获取用户信息")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<PageInfo<UserVO>> getUser(@RequestBody PageRequest<UserGetDTO> userGetDTOPageRequest) {
        PageInfo<UserVO> userVOPage = userService.getUser(userGetDTOPageRequest);
        return ResultUtils.success(userVOPage);
    }

}